using System;
using UnityEngine;

namespace MaterialUI
{
	[AddComponentMenu("MaterialUI/Managers/Dialog Manager")]
	public class DialogManager : MonoBehaviour
	{
		private static DialogManager m_Instance;

		[SerializeField]
		private Canvas m_ParentCanvas;

		private RectTransform m_RectTransform;

		private static DialogManager instance
		{
			get
			{
				if (m_Instance == null)
				{
					m_Instance = new GameObject("DialogManager").AddComponent<DialogManager>();
					m_Instance.InitDialogSystem();
				}
				return m_Instance;
			}
		}

		public static RectTransform rectTransform
		{
			get
			{
				if (instance.m_RectTransform == null)
				{
					instance.m_RectTransform = (instance.transform as RectTransform);
				}
				return instance.m_RectTransform;
			}
		}

		private void Awake()
		{
			if (!m_Instance)
			{
				m_Instance = this;
				m_Instance.InitDialogSystem();
			}
			else
			{
				UnityEngine.Debug.LogWarning("More than one DialogManager exist in the scene, destroying one.");
				UnityEngine.Object.Destroy(base.gameObject);
			}
		}

		private void OnDestroy()
		{
			m_Instance = null;
		}

		private void OnApplicationQuit()
		{
			m_Instance = null;
		}

		private void InitDialogSystem()
		{
			m_RectTransform = base.gameObject.GetAddComponent<RectTransform>();
			if (m_ParentCanvas == null)
			{
				base.transform.SetParent(UnityEngine.Object.FindObjectOfType<Canvas>().transform, worldPositionStays: true);
				m_ParentCanvas = base.transform.GetRootCanvas();
			}
			base.transform.SetParent(m_ParentCanvas.transform, worldPositionStays: false);
			base.transform.localScale = Vector3.one;
			rectTransform.anchorMin = Vector2.zero;
			rectTransform.anchorMax = Vector2.one;
			rectTransform.pivot = new Vector2(0.5f, 0.5f);
			rectTransform.sizeDelta = Vector2.zero;
			rectTransform.localPosition = Vector2.zero;
		}

		public static DialogAlert ShowAlert(string bodyText, string titleText, ImageData icon)
		{
			return ShowAlert(bodyText, null, "OK", titleText, icon);
		}

		public static DialogAlert ShowAlert(string bodyText, Action onAffirmativeButtonClicked, string affirmativeButtonText, string titleText, ImageData icon)
		{
			return ShowAlert(bodyText, onAffirmativeButtonClicked, affirmativeButtonText, titleText, icon, null, null);
		}

		public static DialogAlert ShowAlert(string bodyText, Action onAffirmativeButtonClicked, string affirmativeButtonText, string titleText, ImageData icon, Action onDismissiveButtonClicked, string dismissiveButtonText)
		{
			DialogAlert dialogAlert = CreateAlert();
			dialogAlert.Initialize(bodyText, onAffirmativeButtonClicked, affirmativeButtonText, titleText, icon, onDismissiveButtonClicked, dismissiveButtonText);
			dialogAlert.Show();
			return dialogAlert;
		}

		public static DialogAlert CreateAlert()
		{
			DialogAlert component = PrefabManager.InstantiateGameObject("Dialogs/DialogAlert", instance.transform).GetComponent<DialogAlert>();
			component.Initialize();
			return component;
		}

		public static DialogProgress ShowProgressLinear(string bodyText)
		{
			return ShowProgressLinear(bodyText, null, null);
		}

		public static DialogProgress ShowProgressLinear(string bodyText, string titleText, ImageData icon, bool startStationaryAtZero = false)
		{
			DialogProgress dialogProgress = CreateProgressLinear();
			dialogProgress.Initialize(bodyText, titleText, icon, startStationaryAtZero);
			dialogProgress.ShowModal();
			return dialogProgress;
		}

		public static DialogProgress CreateProgressLinear()
		{
			DialogProgress component = PrefabManager.InstantiateGameObject("Dialogs/DialogProgress", instance.transform).GetComponent<DialogProgress>();
			component.SetupIndicator(isLinear: true);
			component.Initialize();
			return component;
		}

		public static DialogProgress ShowProgressCircular(string bodyText)
		{
			return ShowProgressCircular(bodyText, null, null);
		}

		public static DialogProgress ShowProgressCircular(string bodyText, string titleText, ImageData icon, bool startStationaryAtZero = false)
		{
			DialogProgress dialogProgress = CreateProgressCircular();
			dialogProgress.Initialize(bodyText, titleText, icon, startStationaryAtZero);
			dialogProgress.ShowModal();
			return dialogProgress;
		}

		public static DialogProgress CreateProgressCircular()
		{
			DialogProgress component = PrefabManager.InstantiateGameObject("Dialogs/DialogProgress", instance.transform).GetComponent<DialogProgress>();
			component.SetupIndicator(isLinear: false);
			component.Initialize();
			return component;
		}

		public static DialogSimpleList ShowSimpleList(string[] options, Action<int> onItemClick)
		{
			return ShowSimpleList(options, onItemClick, null, null);
		}

		public static DialogSimpleList ShowSimpleList(string[] options, Action<int> onItemClick, string titleText, ImageData icon)
		{
			OptionDataList optionDataList = new OptionDataList();
			for (int i = 0; i < options.Length; i++)
			{
				OptionData item = new OptionData(options[i], null);
				optionDataList.options.Add(item);
			}
			return ShowSimpleList(optionDataList, onItemClick, titleText, icon);
		}

		public static DialogSimpleList ShowSimpleList(OptionDataList optionDataList, Action<int> onItemClick)
		{
			return ShowSimpleList(optionDataList, onItemClick, null, null);
		}

		public static DialogSimpleList ShowSimpleList(OptionDataList optionDataList, Action<int> onItemClick, string titleText, ImageData icon)
		{
			DialogSimpleList dialogSimpleList = CreateSimpleList();
			dialogSimpleList.Initialize(optionDataList, onItemClick, titleText, icon);
			dialogSimpleList.Show();
			return dialogSimpleList;
		}

		public static DialogSimpleList CreateSimpleList()
		{
			DialogSimpleList component = PrefabManager.InstantiateGameObject("Dialogs/DialogSimpleList", instance.transform).GetComponent<DialogSimpleList>();
			component.Initialize();
			return component;
		}

		public static DialogCheckboxList ShowCheckboxList(string[] options, Action<bool[]> onAffirmativeButtonClicked, string affirmativeButtonText = "OK")
		{
			return ShowCheckboxList(options, onAffirmativeButtonClicked, affirmativeButtonText, null, null);
		}

		public static DialogCheckboxList ShowCheckboxList(string[] options, Action<bool[]> onAffirmativeButtonClicked, string affirmativeButtonText, string titleText, ImageData icon)
		{
			return ShowCheckboxList(options, onAffirmativeButtonClicked, affirmativeButtonText, titleText, icon, null, null);
		}

		public static DialogCheckboxList ShowCheckboxList(string[] options, Action<bool[]> onAffirmativeButtonClicked, string affirmativeButtonText, string titleText, ImageData icon, Action onDismissiveButtonClicked, string dismissiveButtonText)
		{
			DialogCheckboxList dialogCheckboxList = CreateCheckboxList();
			dialogCheckboxList.Initialize(options, onAffirmativeButtonClicked, affirmativeButtonText, titleText, icon, onDismissiveButtonClicked, dismissiveButtonText);
			dialogCheckboxList.Show();
			return dialogCheckboxList;
		}

		public static DialogCheckboxList CreateCheckboxList()
		{
			DialogCheckboxList component = PrefabManager.InstantiateGameObject("Dialogs/DialogCheckboxList", instance.transform).GetComponent<DialogCheckboxList>();
			component.Initialize();
			return component;
		}

		public static DialogRadioList ShowRadioList(string[] options, Action<int> onAffirmativeButtonClicked, string affirmativeButtonText = "OK")
		{
			return ShowRadioList(options, onAffirmativeButtonClicked, affirmativeButtonText, 0);
		}

		public static DialogRadioList ShowRadioList(string[] options, Action<int> onAffirmativeButtonClicked, string affirmativeButtonText, int selectedIndexStart)
		{
			return ShowRadioList(options, onAffirmativeButtonClicked, affirmativeButtonText, null, null, selectedIndexStart);
		}

		public static DialogRadioList ShowRadioList(string[] options, Action<int> onAffirmativeButtonClicked, string affirmativeButtonText, string titleText, ImageData icon)
		{
			return ShowRadioList(options, onAffirmativeButtonClicked, affirmativeButtonText, titleText, icon, null, null);
		}

		public static DialogRadioList ShowRadioList(string[] options, Action<int> onAffirmativeButtonClicked, string affirmativeButtonText, string titleText, ImageData icon, int selectedIndexStart)
		{
			return ShowRadioList(options, onAffirmativeButtonClicked, affirmativeButtonText, titleText, icon, null, null, selectedIndexStart);
		}

		public static DialogRadioList ShowRadioList(string[] options, Action<int> onAffirmativeButtonClicked, string affirmativeButtonText, string titleText, ImageData icon, Action onDismissiveButtonClicked, string dismissiveButtonText, int selectedIndexStart = 0)
		{
			DialogRadioList dialogRadioList = CreateRadioList();
			dialogRadioList.Initialize(options, onAffirmativeButtonClicked, affirmativeButtonText, titleText, icon, onDismissiveButtonClicked, dismissiveButtonText, selectedIndexStart);
			dialogRadioList.Show();
			return dialogRadioList;
		}

		public static DialogRadioList CreateRadioList()
		{
			DialogRadioList component = PrefabManager.InstantiateGameObject("Dialogs/DialogRadioList", instance.transform).GetComponent<DialogRadioList>();
			component.Initialize();
			return component;
		}

		public static T CreateCustomDialog<T>(string dialogPrefabPath) where T : MaterialDialog
		{
			return PrefabManager.InstantiateGameObject(dialogPrefabPath, instance.transform).GetComponent<T>();
		}

		public static void ShowTimePicker(DateTime time, Action<DateTime> onAffirmativeClicked)
		{
			ShowTimePicker(time, onAffirmativeClicked, MaterialColor.teal500);
		}

		public static void ShowTimePicker(DateTime time, Action<DateTime> onAffirmativeClicked, Color accentColor)
		{
			DialogTimePicker component = PrefabManager.InstantiateGameObject("Dialogs/Pickers/DialogTimePicker", instance.transform).GetComponent<DialogTimePicker>();
			component.Initialize(time, onAffirmativeClicked, accentColor);
			component.Show();
		}

		public static void ShowDatePicker(int year, int month, int day, Action<DateTime> onAffirmativeClicked)
		{
			ShowDatePicker(year, month, day, onAffirmativeClicked, MaterialColor.teal500);
		}

		public static void ShowDatePicker(int year, int month, int day, Action<DateTime> onAffirmativeClicked, Color accentColor)
		{
			DialogDatePicker component = PrefabManager.InstantiateGameObject("Dialogs/Pickers/DialogDatePicker", instance.transform).GetComponent<DialogDatePicker>();
			component.Initialize(year, month, day, onAffirmativeClicked, null, accentColor);
			component.Show();
		}

		public static void ShowDatePicker(int year, int month, int day, Action<DateTime> onAffirmativeClicked, Action onDismissiveClicked, Color accentColor)
		{
			DialogDatePicker component = PrefabManager.InstantiateGameObject("Dialogs/Pickers/DialogDatePicker", instance.transform).GetComponent<DialogDatePicker>();
			component.Initialize(year, month, day, onAffirmativeClicked, onDismissiveClicked, accentColor);
			component.Show();
		}

		public static DialogDatePicker CreateDatePicker()
		{
			DialogDatePicker component = PrefabManager.InstantiateGameObject("Dialogs/Pickers/DialogDatePicker", instance.transform).GetComponent<DialogDatePicker>();
			component.Initialize();
			return component;
		}

		public static DialogPrompt ShowPrompt(string firstFieldName, Action<string> onAffirmativeButtonClicked, string affirmativeButtonText, string titleText, ImageData icon, Action onDismissiveButtonClicked, string dismissiveButtonText)
		{
			DialogPrompt dialogPrompt = CreatePrompt();
			dialogPrompt.Initialize(firstFieldName, onAffirmativeButtonClicked, affirmativeButtonText, titleText, icon, onDismissiveButtonClicked, dismissiveButtonText);
			dialogPrompt.Show();
			return dialogPrompt;
		}

		public static DialogPrompt ShowPrompt(string firstFieldName, string secondFieldName, Action<string, string> onAffirmativeButtonClicked, string affirmativeButtonText, string titleText, ImageData icon, Action onDismissiveButtonClicked, string dismissiveButtonText)
		{
			DialogPrompt dialogPrompt = CreatePrompt();
			dialogPrompt.Initialize(firstFieldName, secondFieldName, onAffirmativeButtonClicked, affirmativeButtonText, titleText, icon, onDismissiveButtonClicked, dismissiveButtonText);
			dialogPrompt.Show();
			return dialogPrompt;
		}

		public static DialogPrompt CreatePrompt()
		{
			DialogPrompt component = PrefabManager.InstantiateGameObject("Dialogs/DialogPrompt", instance.transform).GetComponent<DialogPrompt>();
			component.Initialize();
			return component;
		}
	}
}
